package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Praise;
import cn.wolfcode.trip.base.query.PraiseQueryObject;
import cn.wolfcode.trip.base.query.SystemMessagesQueryObject;
import cn.wolfcode.trip.base.service.IPraiseService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/praises")
public class PraiseController {
    @Autowired
    private IPraiseService praiseService;
    //增加赞的功能
    //当点击的时候就增加一个点赞的条数,要传过来的参数有用户id以及type,typeId,用户id
    //如果是取消赞,那么判断
    /*
    某个用户增加某个类型的某个记录的点赞记录
    资源:praises
    动作:post
    参数:praise对象
     */
    @PostMapping
    public Object savePraise(Praise entity){
        //赞/取消赞的时间
        entity.setCreateTime(new Date());
        //都设置赞为未读状态
        entity.setReaded(false);
        JSONResult result = new JSONResult();
        //先查询该用户是否已经为该记录添加了一条点赞数据,
        //如果有,那么我们只修改它的状态,要是,没有就增加一条新记录
        try {
            praiseService.saveOrUpdatePraise(entity);
            result.setData(entity);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
    /*
    查询当前用户某个类型的某个记录的点赞记录
    资源:praises
    动作:get
    参数:
     */
    @GetMapping
    public Object getPraise(Praise entity){
        JSONResult result = new JSONResult();
        try {
            Praise praise = praiseService.get(entity);
            result.setData(praise);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
    /*
    查询某条点评有多少赞
     */
    @GetMapping("/{typeId}")
    public Object getPraiseNum(@PathVariable Long typeId,Integer type){
        Integer num = praiseService.getPraiseNum(typeId,type);
        return num;
    }
    /*
    查询某条点评有多少赞
     */
    @PutMapping
    public Object updatePraise(Praise entity){
        praiseService.updatePraise(entity);
        return new JSONResult();
    }


    /**
     * 查询出点赞信息
     * @param qo
     * @return
     */
    @GetMapping("/user")
    public Object getUserIdLike(PraiseQueryObject  qo){

        return  praiseService.getUserIdLike(qo);
    }

    /**
     * 修改小标签
     * @param qo
     */
    @PutMapping("/readed")
    public void update(PraiseQueryObject qo) {
        praiseService.update(qo);
    }

}
